package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.dao.ChapterMapper;
import top.maof.book.model.Chapter;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.ChapterService;
import top.maof.book.utils.Utils;

@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private ChapterMapper chapterMapper;

    @Override
    public PageData<Chapter> findPage(PageWrap<Chapter> pageWrap) {
        IPage<Chapter> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());

        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Chapter::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getTitle() != null) {
            queryWrapper.lambda().eq(Chapter::getTitle, pageWrap.getModel().getTitle());
        }
        if (pageWrap.getModel().getContent() != null) {
            queryWrapper.lambda().eq(Chapter::getContent, pageWrap.getModel().getContent());
        }
        if (pageWrap.getModel().getUrl() != null) {
            queryWrapper.lambda().eq(Chapter::getUrl, pageWrap.getModel().getUrl());
        }
        if (pageWrap.getModel().getNextUrl() != null) {
            queryWrapper.lambda().eq(Chapter::getNextUrl, pageWrap.getModel().getNextUrl());
        }
        if (pageWrap.getModel().getPreviousUrl() != null) {
            queryWrapper.lambda().eq(Chapter::getPreviousUrl, pageWrap.getModel().getPreviousUrl());
        }
        if (pageWrap.getModel().getUpdateDate() != null) {
            queryWrapper.lambda().ge(Chapter::getUpdateDate, Utils.Date.getStart(pageWrap.getModel().getUpdateDate()));
            queryWrapper.lambda().le(Chapter::getUpdateDate, Utils.Date.getEnd(pageWrap.getModel().getUpdateDate()));
        }
        if (pageWrap.getModel().getContentsId() != null) {
            queryWrapper.lambda().eq(Chapter::getContentsId, pageWrap.getModel().getContentsId());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(chapterMapper.selectPage(page, queryWrapper));
    }

    public int add(Chapter chapter) {
        return chapterMapper.insert(chapter);
    }

    public Chapter find(long id) {
        return chapterMapper.selectByPrimaryKey(id);
    }

    public int update(Chapter chapter) {
        return chapterMapper.updateByPrimaryKeySelective(chapter);
    }

    public int delete(long id) {
        return chapterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Chapter findByUrl(String url) {
        return chapterMapper.findByUrl(url);
    }

    @Override
    public int updateByPrimaryKeySelective(Chapter record) {
        return chapterMapper.updateByPrimaryKeySelective(record);
    }
}
